/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LineMarketResume.java
 *
 * Created on Nov 13, 2011, 3:43:36 PM
 */
package id2212.homework2;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import marketrmi.AccountClientInter;
import marketrmi.ItemInter;
import marketrmi.MarketInter;
import marketrmi.Rejected;

/**
 *
 * @author Guillaume
 */
public class LineMarketResume extends javax.swing.JPanel {

    private ItemInter item;
    private AccountClientInter acc;
    private MarketResume market;

    /** Creates new form LineMarketResume */
    public LineMarketResume(ItemInter item, AccountClientInter acc, MarketResume market) {
        initComponents();
        this.item = item;
        this.acc = acc;
        this.market = market;
        try {
            String sellerName = item.getSellerName();
            jLabel1.setText(item.getItemName());
            jLabel2.setText("" + item.getPrice());
            jLabel3.setText(sellerName);
        } catch (RemoteException ex) {
            Logger.getLogger(LineMarketResume.class.getName()).log(Level.SEVERE, null, ex);
        }

        jButton1.setText("place a wish");
        jButton1.addActionListener(new BtnWishActionListener(item, acc));
        add(jLabel1);
        add(jLabel2);
        add(jLabel3);
        add(jButton1);
        System.out.println("line du market créée");
        setSize(403, 50);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setMaximumSize(new java.awt.Dimension(529, 49));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jLabel1.setText("jLabel1");

        jLabel2.setText("jLabel2");

        jLabel3.setText("jLabel3");

        jButton1.setText("Place a wish");

        jButton2.setText("Buy");
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(52, 52, 52)
                .addComponent(jLabel2)
                .addGap(63, 63, 63)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(jLabel3)
                    .addComponent(jButton2)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Buy bouton
     * @param evt 
     */
    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        try {
            // TODO add your handling code here:
            if (acc.getSellList().contains(item)) {
                new ChangePriceItem(item, acc);
                market.refresh();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(LineMarketResume.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_formMouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        try {
            String str1 = acc.getClientName();
            String str2 = item.getSellerName();
            String passBank = market.getBankPass();
            if (!str1.equalsIgnoreCase(str2)) {
                Client client = market.cl;
                MarketInter mark = client.marketObj;
                mark.buyItem(item, acc, passBank);
                market.refresh();
            }
            else{
                System.out.println("the seller can't buy its own item");
            }
        } catch (Rejected ex) {
            market.setNotifTtext("write your password");
        } catch (RemoteException ex) {
            Logger.getLogger(LineMarketResume.class.getName()).log(Level.SEVERE, null, ex);
            market.setNotifTtext("can't buy item");
        }
    }//GEN-LAST:event_jButton2MouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
